package com.ywj.interview.thread;

public class ThreadLocalDemo2 {
    ThreadLocal<String> t1 = new ThreadLocal<String>();

    //变量
    private String content;

    public String getContent() {
//        return content;
        return t1.get();
    }

    public void setContent(String content) {
//        this.content = content;

        //变量绑定到当前线程
        t1.set(content);
    }

    //使用同步锁synchronized
    public static void main(String[] args) {
        ThreadLocalDemo2 demo = new ThreadLocalDemo2();

        for (int i = 0; i < 5; i++) {
            Thread t = new Thread() {
                @Override
                public void run() {
                    demo.setContent(Thread.currentThread().getName() + "的数据");
                    System.out.println("~~~~~~~~~~~~~~");
                    System.out.println(Thread.currentThread().getName() + "-->" + demo.getContent());
                }
            };
            t.setName("线程" + i);
            t.start();
        }
    }
}
